package com.example.myprojectoffeixue;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
//import de.tavendo.autobahn.WebSocketConnection;

//场景设备列表适配器
public class CjSbadapter extends BaseAdapter {

    private ArrayList<SheBeiClass> d;
    private LayoutInflater layoutInflater;
    private Context context;
    String j;


    public CjSbadapter(ArrayList<SheBeiClass> d, Context context) {
        super();
        this.d = d;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return d.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return d.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder h = null;

        if (convertView == null) {
            h = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_list, null);
            h.tv = (TextView) convertView.findViewById(R.id.tv_listview);
            h.iv = (ImageView) convertView.findViewById(R.id.iv_listview);
            h.iv1 = (ImageView) convertView.findViewById(R.id.iv1_listview);
            h.l = (LinearLayout) convertView.findViewById(R.id.bt_lin);
            // h.l1 = (LinearLayout) convertView.findViewById(R.id.l1_listview);
          /*  if ("1008".equals(cmd)) {
                h.l.setVisibility(View.VISIBLE);
                h.l1.setVisibility(View.GONE);
            } else if ("KT".equals(cmd)) {
                h.l1.setVisibility(View.GONE);
            }
            else if (cmd == null) {
                h.iv1.setClickable(false);
            }*/

            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        if ("".equals(d.get(position).deviceName)) {
            if (d.get(position).deviceId != null) {

                if ("0100".equals(d.get(position).deviceId)) {
                    h.tv.setText("智能开关");
                } else if ("010B".equals(d.get(position).deviceId)||"010A".equals(d.get(position).deviceId)) {
                    h.tv.setText("智能门锁");
                } else if ("010C".equals(d.get(position).deviceId)) {
                    h.tv.setText("取电开关");
                } else if ("0002".equals(d.get(position).deviceId)) {
                    h.tv.setText("智能窗帘");
                } else if ("010F".equals(d.get(position).deviceId)) {
                    h.tv.setText("中央空调");
                } else if ("0009".equals(d.get(position).deviceId)) {
                    h.tv.setText("智能插座");
                }

            }
        } else {
            h.tv.setText(d.get(position).deviceName);
        }

        if (d.get(position).deviceId != null) {

            if ("0100".equals(d.get(position).deviceId)) {
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
                //墙面三路开关
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.kai);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.guan);
                    h.iv1.setImageResource(R.drawable.ankai);

                }


            } else if ("010B".equals(d.get(position).deviceId)||"010A".equals(d.get(position).deviceId)) {
                //智能门锁
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.ms);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.msguan);
                    h.iv1.setImageResource(R.drawable.ankai);

                }


            } else if ("010C".equals(d.get(position).deviceId)) {
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
//取电
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.qudiana);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.qudianguan);
                    h.iv1.setImageResource(R.drawable.ankai);
                }

            } else if ("0002".equals(d.get(position).deviceId)) {
                h.l.setVisibility(View.VISIBLE);
                h.iv1.setVisibility(View.GONE);
                h.iv.setImageResource(R.drawable.cl2);
                // h.l1.setVisibility(View.GONE);
                if ("01".equals(d.get(position).status)) {
                    h.l.setBackgroundResource(R.drawable.clk);
                } else if ("00".equals(d.get(position).status)) {
                    h.l.setBackgroundResource(R.drawable.clg);
                } else if ("06".equals(d.get(position).status)) {

                    h.l.setBackgroundResource(R.drawable.clt);

                }
            } else if ("010F".equals(d.get(position).deviceId)) {

                h.iv.setImageResource(R.drawable.zykt);


            }else if("0009".equals(d.get(position).deviceId)){
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.qudiana);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.chazuo);
                    h.iv1.setImageResource(R.drawable.ankai);
                }





            }









        }

        return convertView;
    }

    class ViewHolder {

        TextView tv;
        ImageView iv;
        LinearLayout l1;
        ImageView iv1;
        LinearLayout l;

    }

}